package com.baohua.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baohua.exception.BaohuaSystemException;
import com.baohua.model.Menu;
import com.baohua.model.Role;
import com.baohua.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {
 
	/**
	 * 用页面异步加载角色列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public @ResponseBody List<Role> queryList(HttpServletRequest request){
	    List<Role> list = roleService.queryRole();
		return list;
	}
	
	
	@RequestMapping("/index")
	public String index() {
		return "role";
	}
	
	/**
	 * 增加角色
	 * @param role
	 * @param request
	 * @return
	 */
	@RequestMapping(value="add", method=RequestMethod.POST)
	public String addRole(Role role, HttpServletRequest request) {
		try {
			//获取传过来的菜单Id字符串
			String menuNOStr = request.getParameter("menuNOs");
			roleService.addRole(role, menuNOStr);
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			//将错误抛出到后台
			request.setAttribute("errorMessage", e.getMessage());
    		return "forward:/system/error";
		}
		
		return "redirect:/role/index";
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @param request
	 * @return
	 */
	@RequestMapping(value="update", method=RequestMethod.POST)
	public String updateRole(Role role, HttpServletRequest request) {
		try {
			//获取传过来的菜单Id字符串
			String menuNOStr = request.getParameter("menuNOs");
			roleService.updateRole(role, menuNOStr);
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			//将错误抛出到后台
			request.setAttribute("errorMessage", e.getMessage());
    		return "forward:/system/error";
		}
		
		return "redirect:/role/index";
	}
	
	/**
	 * 删除角色
	 * @param roleId
	 * @param model
	 * @return
	 */
	@RequestMapping("delete/{roleId}")
	public String deleteRole(@PathVariable int roleId, Model model) {
		try {
			roleService.delRole(roleId);
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
			//将错误抛出到后台
			model.addAttribute("errorMessage", e.getMessage());
    		return "forward:/system/error";
		}
		
		return "redirect:/role/index";
	}
	
	/**
	 * 根据roleId查询角色所对应的菜单树
	 * @param roleId
	 * @return
	 */
	@RequestMapping("queryMenuList/{roleId}")
	public @ResponseBody Set<Menu> queryMenuByRoleId(@PathVariable int roleId) {
		Set<Menu> menus = new HashSet<Menu>();
		try {
			menus = roleService.queryMenuByRoleId(roleId);
		} catch (BaohuaSystemException e) {
			logger.info(e.getMessage());
		}
		return menus;
	}
	
	
	private static final Logger logger = Logger.getLogger(RoleController.class.getName());
	private RoleService roleService;
	public RoleService getRoleService() {
		return roleService;
	}

	@Resource(name="roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	
}
